package CtrlLayer;
import java.util.ArrayList;
import ModelLayer.*;

public class OrderController{
    private OrderContainer orderContainer;
    private ProductController productController;

    public OrderController(){
        orderContainer = OrderContainer.getInstance();
        productController = new ProductController();
    }

    public int createOrder(int sessionId){  
        int employeeId = SessionContainer.getInstance().findSessionById(sessionId).getPersonId();
        Order oObject = null;
        int index = 0;
        int id = 0;
        while(index < orderContainer.getOrderList().size())
        {
            oObject = orderContainer.getOrderList().get(index);
            if(oObject !=null){
                if(oObject.getId()>id)   
                    id = oObject.getId();
            }else{
                id = 1;
            }
            index++;
        }
        id++;
        orderContainer.addOrder(new Order(id, employeeId));
        return id;
    }

    public int createOrderAutomatically(int saleId, int sessionId){  
        int employeeId = SessionContainer.getInstance().findSessionById(sessionId).getPersonId();
        Order oObject = null;
        int index = 0;
        int id = 0;
        while(index < orderContainer.getOrderList().size())
        {
            oObject = orderContainer.getOrderList().get(index);
            if(oObject !=null){
                if(oObject.getId()>id)   
                    id = oObject.getId();
            }else{
                id = 1;
            }
            index++;
        }
        id++;
        orderContainer.addOrder(new Order(id, employeeId));
        orderContainer.findOrderById(id).addProductInOrderAutomatically(saleId);
        return id;
    }

    public boolean addProductInOrderManually(int id, int productBarcode, int amount){
        if(!orderContainer.findOrderById(id).isReady() && productController.findProduct(productBarcode) != null) {
            orderContainer.findOrderById(id).addProductInOrderManually(productBarcode, amount);
            productController.findProduct(productBarcode).recalculateOrderAmount(amount);
            return true;
        } else {
            return false;
        }
    }

    public boolean removeProductFromOrderManually(int id, int productBarcode,int amount){
        if(!orderContainer.findOrderById(id).isReady() && productController.findProduct(productBarcode) != null) {
            orderContainer.findOrderById(id).removeProductFromOrderManually(productBarcode,amount);
            productController.findProduct(productBarcode).recalculateOrderAmount(-amount);
            return true;
        } else {
            return false;
        }
    }

    public boolean endOrder(int id){
        orderContainer.findOrderById(id).setReady();
        if(orderContainer.findOrderById(id).isReady()) {
            return true;
        } else {
            return false;
        }
    }

    public Order findOrder(int id){    
        return  orderContainer.findOrderById(id);
    }

    public void deleteOrder(int id){
        orderContainer.deleteOrder(orderContainer.findOrderById(id));
    }
}
